<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/5 0005
 * Time: 17:08
 */

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Illuminate\Support\Str;

class UploadController extends Controller
{
    protected $storage = '';

    private $maximum = '5120';

    private $media_maximum = '10240';

    /**
     * File name.
     *
     * @var null
     */
    protected $name = null;

    public function store(Request $request)
    {

        $res = [
            'code' => -1,
            'message' => trans('api.illegal_parameter'),
            'status' => false,
        ];
        if ($request->hasFile('image_file')) {

            $file = $request->file('image_file');
            $data = $request->all();
            $rules = [
                'image_file' => 'max:' . $this->maximum,
            ];
            $messages = [
                'image_file.max' => trans('api.over_maximum') . ($this->maximum / 1024) . 'M',
            ];

            $validator = Validator($data, $rules, $messages);

            if ($validator->passes()) {
                $ext = $file->getClientOriginalExtension();
                $check_ext = in_array($ext, ['gif', 'jpg', 'jpeg', 'png'], true);
                if ($check_ext) {
                    $this->disk(config('admin.upload.disk'));
                    $this->name = 'images/' . $this->generateUniqueName($file);
                    $this->storage->put($this->name, file_get_contents($file->getRealPath()));

                    if ($file->isValid()) {
                        $res['data']['image'] = $this->objectUrl($this->name);
                        $res['message'] = trans('api.upload_succeed');
                        $res['code'] = 0;
                        $res['status'] = true;
                    } else {
                        $res['message'] = trans('api.upload_failed') . ':' . trans('api.validate_failed');
                        $res['code'] = 3;
                        $res['status'] = false;
                    }

                } else {
                    $res['message'] = trans('api.upload_failed') . ':' . trans('api.accept') . ':(gif、jpg、jpeg and png)';
                    $res['code'] = 2;
                    $res['status'] = false;
                }
            } else {
                $res['message'] = $validator->messages()->first();
                $res['code'] = 1;
                $res['status'] = false;
            }
        }

        if ($request->hasFile('video_file')) {

            $file = $request->file('video_file');
            $data = $request->all();
            $rules = [
                'video_file' => 'max:' . $this->media_maximum,
            ];
            $messages = [
                'video_file.max' => trans('api.over_maximum') . $this->media_maximum . 'k',
            ];

            $validator = Validator($data, $rules, $messages);

            if ($validator->passes()) {
                $ext = $file->getClientOriginalExtension();
                $check_ext = in_array($ext, ['mp4', 'avi', 'wmv', 'flv', 'mov', 'rmvb'], true);
                if ($check_ext) {
                    $this->disk(config('admin.upload.disk'));
                    $this->name = 'video/' . $this->generateUniqueName($file);
                    $this->storage->put($this->name, file_get_contents($file->getRealPath()));

                    if ($file->isValid()) {
                        $res['data']['video'] = $this->objectUrl($this->name);
                        $res['message'] = trans('api.upload_succeed');
                        $res['code'] = 0;
                        $res['status'] = true;
                    } else {
                        $res['message'] = trans('api.upload_failed') . ':' . trans('api.validate_failed');
                        $res['code'] = 3;
                        $res['status'] = false;
                    }

                } else {
                    $res['message'] = trans('api.upload_failed') . ':' . trans('api.accept') . ':(mp4, avi, wmv, flv, mov, rmvb)';
                    $res['code'] = 2;
                    $res['status'] = false;
                }
            } else {
                $res['message'] = $validator->messages()->first();
                $res['code'] = 1;
                $res['status'] = false;
            }
        }

        return $res;
    }

    public function disk($disk)
    {
        $this->storage = Storage::disk($disk);

        return $this;
    }

    public function objectUrl($path)
    {
        // 如果 image 字段本身就已经是完整的 url 就直接返回
        if (Str::startsWith($path, ['http://', 'https://'])) {
            return $path;
        }

        return Storage::disk(config('admin.upload.disk'))->url($path);
    }

    protected function generateUniqueName(UploadedFile $file)
    {
        return md5(uniqid()) . '.' . $file->guessExtension();
    }
}
